package org.jiuling.wecom.school;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.verify.ListVerify;
import org.jiuling.local.tools.verify.LongVerify;
import org.jiuling.local.tools.verify.ObjectVerify;
import org.jiuling.network.tools.http.request.HttpRequest;
import org.jiuling.network.tools.http.request.enums.RequestMethodEnum;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.model.HttpRequestParam;
import org.jiuling.wecom.basics.Authorization;
import org.jiuling.wecom.config.WeComConfig;
import org.jiuling.wecom.school.enums.WeComSchoolApiEnum;
import org.jiuling.wecom.school.model.DivisionalModel;
import org.jiuling.wecom.school.verify.RequestVerify;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门管理
 * @author 紫灵
 * @date 2023年04月03日 09时56分10秒
 */
public class DivisionalManage {

    /** 授权 */
    private final Authorization authorization;

    public DivisionalManage(Authorization authorization) {
        this.authorization = authorization;
    }

    /**
     * 获取部门列表
     * @param divisionalId 部门id
     * @return 部门列表
     */
    public List<DivisionalModel> getDivisionalList(Long divisionalId){
        HttpRequestParam httpRequestParam = new HttpRequestParam()
                .add("access_token",authorization.getToken().getAccessToken());
        if(!LongVerify.isBlank(divisionalId)){
            httpRequestParam.add("id",divisionalId);
        }
        HttpRequest httpRequest = new HttpRequest();
        try {
            HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory(WeComSchoolApiEnum.GET_DEPARTMENT_LIST.getApi());
            httpRequestFactory.setRequestParam(httpRequestParam);
            httpRequestFactory.setRequestMethod(RequestMethodEnum.GET);
            JSONObject jsonObject = JSON.parseObject(httpRequest.send().getBody());
            RequestVerify.errorVerify(jsonObject);
            // 部门
            JSONArray departmentJsonArray = jsonObject.getJSONArray("departments");
            List<DivisionalModel> divisionalModelList = new ArrayList<>();
            if(!ObjectVerify.isNull(departmentJsonArray) && !departmentJsonArray.isEmpty()){
                for (int i = 0; i < departmentJsonArray.size(); i++) {
                    divisionalModelList.add(DivisionalModel.builder().addDivisionalJson(departmentJsonArray.getJSONObject(i)).build());
                }
            }
            return divisionalModelList;
        } catch (RequestException | IOException | LocalToolsException | NoSuchAlgorithmException |
                 KeyManagementException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        WeComConfig weComConfig = WeComConfig.builder()
                .addAgentId("1000003")
                .addCorpId("wwb8f187e1b4973254")
                .addCorpSecret("4Y-oi0dE8pRKUVjtoWI-Z3oOvv-9in2RPkdZ2S-sDA8")
                .build();
        Authorization authorization = new Authorization(weComConfig);
        DivisionalManage divisionalManage = new DivisionalManage(authorization);
        divisionalManage.getDivisionalList(2L).forEach(divisionalManage1 ->{
            System.out.println("部门id："+divisionalManage1.getId()+" 名称："+divisionalManage1.getName()+" 父级id："+divisionalManage1.getParentId()+" 类型："+divisionalManage1.getType().getCode());
            List<DivisionalModel.DepartmentAdminModel> departmentAdminModelList = divisionalManage1.getDepartmentAdminModelList();
            if(!ListVerify.isEmpty(departmentAdminModelList)) {
                divisionalManage1.getDepartmentAdminModelList().forEach(departmentAdminModel -> {
                    System.out.println(departmentAdminModel.getUserId());
                });
            }
        });
    }

}
